package com.jk.vehicle.mini.util;

import com.jk.vehicle.core.utils.ReplaceUtil;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;

/**
 * 转义DTO中字符串中的特殊字符
 * @author cjc
 * @date 2018/12/27
 */
@Slf4j
public class DTOUtil {

	/**
	 *String 类型
	 */
	private static String type = "class java.lang.String";

	/**
	 * 去空格
	 * @param o 对象
	 */
	public static void trimObject(Object o) {
		Field[] fields = o.getClass().getDeclaredFields();
		Object data;
		for (Field field : fields) {
			if (type.equals(field.getGenericType().toString())) {
				field.setAccessible(true);
				try {
					data = field.get(o);
					if (data != null) {
						field.set(o, data.toString().trim());
					}
				} catch (IllegalAccessException e) {
					log.error(e.getMessage());
				}
			}
		}
	}

	/**
	 * 转义特殊字符
	 * @param o 对象
	 */
	public static void tranMeanSpecialString(Object o) {
		Field[] fields = o.getClass().getDeclaredFields();
		Object data;
		for (Field field : fields) {
			if (type.equals(field.getGenericType().toString())) {
				field.setAccessible(true);
				try {
					data = field.get(o);
					if (data != null) {
						field.set(o, ReplaceUtil.replace(data.toString()));
					}
				} catch (IllegalAccessException e) {
					log.error(e.getMessage());
				}
			}
		}
	}
}
